package com.vclee.fast.integration.power;

import com.alibaba.fastjson.JSONObject;
import com.vclee.fast.integration.power.model.SysUser;
import com.vclee.fast.integration.power.model.vo.AdmSysUserPageQuery;
import com.vclee.fast.integration.power.service.SysUserService;
import com.vclee.fast.utils.aspect.SysLog;
import com.vclee.fast.utils.exception.Assert;
import com.vclee.fast.utils.exception.R;
import com.vclee.fast.utils.model.Bson;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@RequestMapping("admin-sysUser")
public class SysUserController {

    private final SysUserService sysUserService;

    @SysLog("商家控制台及综合管理平台登录")
    @PostMapping("/adminLogin")
    public R adminLogin(@RequestBody JSONObject params) {
        String unique = params.getString("unique");
        String password = params.getString("password");
        int flag = params.getIntValue("flag"); // 默认0 表示商家端登录 1 表示后台管理端登录
        return R.ok(sysUserService.adminLogin(unique,password,flag));
    }

    @SysLog("新增系统用户")
    @PostMapping("/addSysUser")
    public R addSysUser(@RequestBody SysUser sysUser) {
        sysUser.setCreateTime(new Date());
        sysUserService.editUser(sysUser);
        return R.ok();
    }

    @SysLog("修改系统用户")
    @PostMapping("/modifySysUser")
    public R modifySysUser(@RequestBody SysUser sysUser) {
        sysUserService.editUser(sysUser);
        return R.ok();
    }

    @SysLog("查询系统用户列表")
    @PostMapping("/getAdmSysUserList")
    public R getAdmSysUserList(@RequestBody AdmSysUserPageQuery query) {
        return R.ok(sysUserService.getAdmSysUserList(query));
    }

    @SysLog("后台查询系统用户角色列表")
    @PostMapping(value = "/findUserRoleList")
    public R findUserRoleList(@RequestBody Bson query) {
        Integer userId = query.getInteger("userId");
        Integer flag = query.getInteger("flag"); // 0 仅查询用户角色 1 查询用户所有角色
        return R.ok(sysUserService.findUserRoleList(userId,flag));
    }

    @SysLog("后台添加用户角色")
    @PostMapping(value = "/addUserRole")
    public R addUserRole(@RequestBody Bson query) {
        String roleIds = query.getString("roleIds");
        Integer userId = query.getInteger("userId");
        Integer flag = query.getInteger("flag"); // 1 设置权限
        Integer orgId = query.getInteger("orgId");
        List<Integer> list = new ArrayList<>();
        if(!Assert.isBlank(roleIds)){
            list = Arrays.stream(roleIds.split(","))
                    .map(Integer::parseInt).collect(Collectors.toList());
        }
        sysUserService.addUserRole(userId, list, flag,orgId);
        return R.ok();
    }

    @SysLog("查询登录用户菜单列表")
    @PostMapping(value = "/findUserMenuList")
    public R findUserMenuList(@RequestBody JSONObject query) {
        Integer userId = query.getInteger("userId");
        return R.ok(sysUserService.findUserMenuList(userId));
    }

}
